home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 312_01 / pincdep.awk < prev    next >
Text File  |  1990-04-21  |  13KB  |  284 lines

  1. #  HEADER:        ;
  2. #  TITLE:         Makefile dependency generator;
  3. #  DATE:          09/28/1989;
  4. #  VERSION:       1.2;
  5. #  DESCRIPTION:   "An AWK program which finds included files in a PL/M
  6. #                 source file and builds a dependency list.  This
  7. #                 dependency list is a component of a make-file.  The list 
  8. #                 consists of the source file name followed by a colon and
  9. #                 then all of the files which it includes (and also
  10. #                 those which are included by the included files, &c.)
  11. #                 The last item is a compile-command.";
  12. #
  13. #  KEYWORDS:      Makefile, make, include, dependency generator;
  14. #  SYSTEM:        MS-DOS, UNIX;
  15. #  WARNINGS:      "Doesn't handle directory names embedded within
  16. #                 $include(here).  Doesn't handle preprocessor $if.. blocks
  17. #                 or $include statements which are "commented out."
  18. #                 PolyAwk will generate false matches to lines
  19. #                 beginning with non-ASCII (i.e. 128..256) chars."
  20. #                 Filenames are case sensitive.";
  21. #  FILENAME:      PINCDEP.AWK;
  22. #  SEE-ALSO:      TLR2MAK.AWK, CF2MAK.AWK, CINCDEP.AWK, LBI2MAK.AWK;
  23. #  AUTHORS:       James Yehle;
  24. #  COMPILERS:     PolyAWK, Mortice Kern AWK, Rob Duff's PC-AWK 2.0,
  25. #                 In any case, must be 1985 awk (not 1977);
  26. # ============================================================================
  27. #
  28. # pincdep.awk         Last modified  Sep 28, 1989  22:30
  29. #
  30. #   Scans a PL/M source file for all "$include" dependencies
  31. #   This scanning process involves all nested include files, too.
  32. #
  33. #   Usage is:
  34. #     awk -f [path]pincdep.awk [path]src_file.c objname=[path]filename
  35. #        cc=compile_comd ccf=[p][n][e] [>[path]outfile]
  36. #     Pathnames must contain trailing separator, e.g. "c:\inc\"
  37. #     Order of command-line parameters (save for "-f ..." 
  38. #      and srcfile.c) is irrelevant, but identifiers must be in lower case.
  39. #     "src_file.c" may be replaced by special "__NOFILE__"
  40. #     cc is the compile command
  41. #     ccf controls whether directory path (p) precedes filename in
  42. #         the compile command line, and whether extension is added (e)
  43. #
  44. #   Output is:
  45. #     [path]src_file.obj: [path]src_file.c [path]included_file1 linecont
  46. #                         [path]included_file2 [path]included_file3 ...
  47. #     <\t>comd [path]src_file.c
  48. #     <\n>
  49. #   For __NOFILE__, output is:
  50. #     [path]src_file.obj:
  51. #     <\n>
  52. #     <\n>
  53. #
  54. #  Jim Yehle
  55. #
  56. # . . . Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . .
  57. #
  58. #  1.2  Sep 10 89  JRY  Added legal-filename-char regular expression
  59. #  1.1  Aug 17 89  JRY  Added add_fnexist option (as internal directive)
  60. #                       If an included file can't be found, pincdep now
  61. #                        beeps and prints an error to CO, then continues
  62. #                        without scanning the file for nested inclusions.
  63. #                        (It adds it to the dependency list.)
  64. #                        Also added an optimization:  if addfile() sees
  65. #                        that a file is already in the list, then it has
  66. #                        already been scanned, so it isn't done again.
  67. #                       Added "verbose" CO output as debug level 1.
  68. #  1.0  Jul 23 89  JRY  Made pincdep for PL/M files; adapted from
  69. #                        Intel asm286 assembly dependency scanner 
  70. #                        aincdep.awk, which was in turn adapted from
  71. #                        the original C version cincdep.awk.
  72. # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  73. #
  74. # Notes about this program:
  75. #  It assumes Intel PLM286 compiler syntax
  76. #  It doesn't make any attempt to switch out $If...$Endif blocks.
  77. #  It is powerless to cover files $Included in the compiler's command-line
  78. #  It has no facilities for an include-file directory to be specified
  79. #   (since iRMX's PL/M compiler has none)
  80. #
  81. # Here are the syntax rules for $Include() (or $IC()):
  82. #  Control lines begin with $ in column 1 (or in LEFTCOL)
  83. #   and end with or end-of-line or a semicolon before end-of-line
  84. #  Multiple controls (immediately) follow the $, separated by space/tabs
  85. #  Only one "include" control is allowed per line
  86. #
  87. # What I can't deal with:
  88. #  "include(...)" as a parameter within some other control
  89. #   e.g. "title 'include(blah)' "
  90. #  LEFTCOL settings other than 1
  91. # ============================================================================
  92. #
  93.  
  94. BEGIN {
  95.    co = "CON"       # Console-out: MS-DOS "CON", iRMX ":co:", unix "/dev/tty"
  96.    linecont = " \\" # Line-continuation EOL char: snake " /", unix make "\\"
  97.    debug = 0        # 0=off, 1=verbose, 2=main-level debug, 3= adds functions
  98.    use_fnexist = 1  # Add nonexistent header files to dependency list?
  99.    fname_chars = "[^\\\\ :]" # Any single legal char in a file name.
  100.    #  (Must exclude directory path separator character! (DOS \, UNIX /) )
  101.  
  102.    printf( "pincdep.awk  PL/M $include dependency scanner  v 1.2") > co
  103.    objfile = get_cl_param( "objname=", 0)
  104.    if (debug>1) printf( "objfile = '%s'\n", objfile) > co
  105.    cc = get_cl_param( "cc=", 0)
  106.    if (debug>1) printf( "cc = '%s'\n", cc) > co
  107.    ccf = get_cl_param( "ccf=", 0)
  108.    if (debug>1) printf( "ccf = '%s'\n", ccf) > co
  109.    if (ARGV[1] == "__NOFILE__") {
  110.       if (debug)                    
  111.          printf( "\n__NOFILE__: Empty dependency list generated.\n") > co
  112.       else
  113.          printf( "  (of __NOFILE__)\n") > co
  114.       exit # Hop to END; srcfile is still an empty string & flist is empty
  115.    }
  116.    # Split primary source file name into Path, Name, and Extension components
  117.    # 1st in list of ARGV[]'s is source file (FILENAME not set in BEGIN section)
  118.    fullsrcfile = ARGV[1]
  119.    split_filename( fullsrcfile, srcfile, fname_chars)
  120.    if (debug)
  121.       printf( "\n Scanning primary source file '%s%s%s'\n",
  122.               srcfile["path"], srcfile["name"], srcfile["ext"]) > co
  123.    else
  124.       printf( "  (of %s%s%s)\n",
  125.               srcfile["path"], srcfile["name"], srcfile["ext"]) > co
  126.    if (debug>1) printf( "srcfile[\"path\"] = \"%s\"\n", srcfile["path"]) > co
  127.    if (debug>1) printf( "srcfile[\"name\"] = \"%s\"\n", srcfile["name"]) > co
  128.    if (debug>1) printf( "srcfile[\"ext\"] = \"%s\"\n", srcfile["ext"]) > co
  129. }
  130. #
  131. /^\$[^;]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][ \t]*\(.*\)/ {
  132.    if (debug>1) printf( "$include control line: $0='%s'\n", $0) > co
  133.    extract_include_filename()  # into $1
  134.    if (debug>1) printf( " extracted filename = '%s'\n", $1) > co
  135.    # Check for nested inclusions by scanning included file for $Include's
  136.    scanfile( $1)
  137. }
  138. /^\$[^;]*[Ii][Cc][ \t]*\(.*\)/ {
  139.    if (debug>1) printf( "$include control line: $0='%s'\n", $0) > co
  140.    extract_ic_filename()  # into $1
  141.    if (debug>1) printf( " extracted filename = '%s'\n", $1) > co
  142.    # Check for nested inclusions by scanning included file for $Include's
  143.    scanfile( $1)
  144. }
  145.  
  146. # Produce the dependency list printout
  147. END {
  148.    # Print primary (object) file name before list (left of colon),
  149.    # Print primary source file name (*.c) as first item in list.
  150.    printf( "%s: %s", objfile, fullsrcfile )
  151.    if (debug)  printf( "%s: %s", objfile, fullsrcfile) >co
  152.    linelen = length(objfile) + 2 + length(fullsrcfile)
  153.  
  154.    for (i=1; i in flist; i++) {
  155.       # If line will be too long, put out a line-continuation char and newline
  156.       if (linelen + 1 + length(flist[i]) + length(linecont)  > 79) {
  157.          printf( "%s\n", linecont)
  158.          if (debug)  printf( "%s\n", linecont) >co
  159.          # On the new line, tab out to underneath the first included file name
  160.          for (linelen=0; linelen<length(objfile)+1; ++linelen) {
  161.             printf(" ")
  162.             if (debug)  printf(" ") >co
  163.          }
  164.       }
  165.       printf( " %s",  flist[i] )
  166.       if (debug)  printf( " %s",  flist[i] ) >co
  167.       linelen += length(flist[i]) + 1
  168.       }
  169.  
  170.    # Build the compile command line's only parameter: the filename
  171.    #  depending on options specified in ccf command-line param
  172.    compile_comd_filename